package com.ei.itop.stat.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ailk.dazzle.util.ibatis.GenericDAO;
import com.ailk.dazzle.util.type.DateUtils;
import com.ei.itop.common.bean.OpInfo;
import com.ei.itop.incidentmgnt.bean.QCIncident;
import com.ei.itop.stat.bean.IncidentAnalysisReport;
import com.ei.itop.stat.bean.IncidentReport;
import com.ei.itop.stat.bean.IncidentStateCount;
import com.ei.itop.stat.bean.IncidentStateReport;
import com.ei.itop.stat.bean.StateCount;
import com.ei.itop.stat.service.ReportService;

@Service
public class ReportServiceImpl implements ReportService{

	@Resource(name = "app.siCommonDAO")
	private GenericDAO<Long, IncidentReport> incidentDAO;
	
	@Resource(name = "app.siCommonDAO")
	private GenericDAO<String, IncidentStateCount> incidentWeekStatDAO;
	
	@Resource(name = "app.siCommonDAO")
	private GenericDAO<String, IncidentAnalysisReport> incidentAnalysisReportDAO;
	
	@Resource(name = "app.siCommonDAO")
	private GenericDAO<String, StateCount> stateCountDAO;

	public List<IncidentReport> MBLQueryIncident4Report(QCIncident qi, long startIndex, int pageSize, OpInfo oi,String locale) throws Exception {
		HashMap<String, Object> hm = new HashMap<String, Object>();
		String incidentCode = qi.getIncidentCode() == null ? ""
				: qi.getIncidentCode();
		hm.put("incidentCode", "%" + incidentCode + "%");
		hm.put("brief", "%" + (qi.getBrief() == null ? "" : qi.getBrief()) + "%");
		hm.put("keywords", qi.getKeywords());
		hm.put("orgId", qi.getOrgId());
		hm.put("custId", qi.getCustId());
		hm.put("registeManId", qi.getRegisteManId());
		hm.put("adviserId", qi.getAdviserId());
		hm.put("productId", qi.getProductId());
		hm.put("affectCode", qi.getAffectCode());
		hm.put("classCode", qi.getClassCode());
		hm.put("priorityCode", qi.getPriorityCode());
		hm.put("complexCode", qi.getComplexCode());
		hm.put("stateCode", qi.getStateCode());
		hm.put("registerTimeBegin", qi.getRegisterTimeBegin());
		hm.put("registerTimeEnd", qi.getRegisterTimeEnd());
		hm.put("dealDur2Begin", qi.getDealDur2Begin());
		hm.put("dealDur2End", qi.getDealDur2End());

		if (!"desc".equals(qi.getOrderByRegisterTime())
				&& !"asc".equals(qi.getOrderByRegisterTime())) {
			qi.setOrderByRegisterTime("desc");
		}
		if (!"desc".equals(qi.getOrderByLastModifyTime())
				&& !"asc".equals(qi.getOrderByLastModifyTime())) {
			qi.setOrderByLastModifyTime("desc");
		}

		hm.put("orderBy", "registe_time " + qi.getOrderByRegisterTime()
				+ ", modify_date " + qi.getOrderByLastModifyTime());
		hm.put("startIndex", startIndex);
		hm.put("locale", locale);

		List<IncidentReport> incidentList = null;

		// 不分页
		if (startIndex == -1) {
			incidentList = incidentDAO.findByParams(
					"IC_INCIDENT.queryIncident4Report", hm);
		}
		// 分页
		else {
			long endIndex = startIndex + pageSize - 1;
			hm.put("endIndex", endIndex);

			incidentList = incidentDAO.findByParams(
					"IC_INCIDENT.queryIncidentPaging4Report", hm);
		}

		return incidentList;
	}

	public List<IncidentStateReport> MBLQueryIncidentSumReport(String adviserId, Date expStartDate, Date expEndDate) throws Exception {
		//上周周末时间
		String lastWeekEndDateStr = DateUtils.date2String(DateUtils.dateOffset(expStartDate, Calendar.DAY_OF_YEAR, -1), DateUtils.FORMATTYPE_yyyy_MM_dd);
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("adviserId", adviserId);
		//查询周初未完成时间，即上周的周末没有完成的事件
		params.put("weekEndDateStr", lastWeekEndDateStr);
		params.put("statType", 1);
		List<IncidentStateCount> beginUnsolvedData = incidentWeekStatDAO.findByParams("XC_INCIDENT.queryIncidentStateCount", params);
		//查询本周内收到的事件
		params.put("weekEndDateStr", DateUtils.date2String(expEndDate, DateUtils.FORMATTYPE_yyyy_MM_dd));
		params.put("statType", 2);
		List<IncidentStateCount> receivedData = incidentWeekStatDAO.findByParams("XC_INCIDENT.queryIncidentStateCount", params);
		//查询本周内解决的事件
		params.put("statType", 3);
		List<IncidentStateCount> solvedData = incidentWeekStatDAO.findByParams("XC_INCIDENT.queryIncidentStateCount", params);
		//查询本周末未解决的事件
		params.put("statType", 1);
		List<IncidentStateCount> endUnsolvedData = incidentWeekStatDAO.findByParams("XC_INCIDENT.queryIncidentStateCount", params);
		//拼装结果集
		Map<Integer,IncidentStateReport> mapData = new HashMap<Integer, IncidentStateReport>();
		for(IncidentStateCount sc:beginUnsolvedData){
			IncidentStateReport sr = new IncidentStateReport();
			sr.setCustId(sc.getCustId());
			sr.setCustName(sc.getCustName());
			sr.setBeginUnSolvedCount1(sc.getProdTypeCount1());
			sr.setBeginUnSolvedCount2(sc.getProdTypeCount2());
			sr.setBeginUnSolvedCount3(sc.getProdTypeCount3());
			sr.setBeginUnSolvedCountX(sc.getProdTypeCountX());
			sr.setBeginUnSolvedSumCount(sc.getProdTypeSumCount());
			mapData.put(sc.getCustId(), sr);
		}
		for(IncidentStateCount sc:receivedData){
			IncidentStateReport sr = mapData.get(sc.getCustId());
			if(sr==null){
				sr = new IncidentStateReport();
				sr.setCustId(sc.getCustId());
				sr.setCustName(sc.getCustName());
				sr.setBeginUnSolvedCount1(0);
				sr.setBeginUnSolvedCount2(0);
				sr.setBeginUnSolvedCount3(0);
				sr.setBeginUnSolvedCountX(0);
				sr.setBeginUnSolvedSumCount(0);
				mapData.put(sc.getCustId(), sr);
			}
			sr.setRecCount1(sc.getProdTypeCount1());
			sr.setRecCount2(sc.getProdTypeCount2());
			sr.setRecCount3(sc.getProdTypeCount3());
			sr.setRecCountX(sc.getProdTypeCountX());
			sr.setRecSumCount(sc.getProdTypeSumCount());
		}
		for(IncidentStateCount sc:solvedData){
			IncidentStateReport sr = mapData.get(sc.getCustId());
			if(sr==null){
				sr = new IncidentStateReport();
				sr.setCustId(sc.getCustId());
				sr.setCustName(sc.getCustName());
				sr.setBeginUnSolvedCount1(0);
				sr.setBeginUnSolvedCount2(0);
				sr.setBeginUnSolvedCount3(0);
				sr.setBeginUnSolvedCountX(0);
				sr.setBeginUnSolvedSumCount(0);
				sr.setRecCount1(0);
				sr.setRecCount2(0);
				sr.setRecCount3(0);
				sr.setRecCountX(0);
				sr.setRecSumCount(0);
				mapData.put(sc.getCustId(), sr);
			}
			sr.setSolvedCount1(sc.getProdTypeCount1());
			sr.setSolvedCount2(sc.getProdTypeCount2());
			sr.setSolvedCount3(sc.getProdTypeCount3());
			sr.setSolvedCountX(sc.getProdTypeCountX());
			sr.setSolvedSumCount(sc.getProdTypeSumCount());
		}
		for(IncidentStateCount sc:endUnsolvedData){
			IncidentStateReport sr = mapData.get(sc.getCustId());
			if(sr==null){
				sr = new IncidentStateReport();
				sr.setCustId(sc.getCustId());
				sr.setCustName(sc.getCustName());
				sr.setBeginUnSolvedCount1(0);
				sr.setBeginUnSolvedCount2(0);
				sr.setBeginUnSolvedCount3(0);
				sr.setBeginUnSolvedCountX(0);
				sr.setBeginUnSolvedSumCount(0);
				sr.setRecCount1(0);
				sr.setRecCount2(0);
				sr.setRecCount3(0);
				sr.setRecCountX(0);
				sr.setRecSumCount(0);
				sr.setSolvedCount1(0);
				sr.setSolvedCount2(0);
				sr.setSolvedCount3(0);
				sr.setSolvedCountX(0);
				sr.setSolvedSumCount(0);
				mapData.put(sc.getCustId(), sr);
			}
			sr.setEndUnsolvedCount1(sc.getProdTypeCount1());
			sr.setEndUnsolvedCount2(sc.getProdTypeCount2());
			sr.setEndUnsolvedCount3(sc.getProdTypeCount3());
			sr.setEndUnsolvedCountX(sc.getProdTypeCountX());
			sr.setEndUnsolvedSumCount(sc.getProdTypeSumCount());
		}
		//算合计
		List<IncidentStateReport> result = new ArrayList<IncidentStateReport>(mapData.values());
		if(result.size()>0){
			IncidentStateReport totalSum = new IncidentStateReport();
			totalSum.setCustName("汇总");
			totalSum.setBeginUnSolvedCount1(0);
			totalSum.setBeginUnSolvedCount2(0);
			totalSum.setBeginUnSolvedCount3(0);
			totalSum.setBeginUnSolvedCountX(0);
			totalSum.setBeginUnSolvedSumCount(0);
			totalSum.setRecCount1(0);
			totalSum.setRecCount2(0);
			totalSum.setRecCount3(0);
			totalSum.setRecCountX(0);
			totalSum.setRecSumCount(0);
			totalSum.setSolvedCount1(0);
			totalSum.setSolvedCount2(0);
			totalSum.setSolvedCount3(0);
			totalSum.setSolvedCountX(0);
			totalSum.setSolvedSumCount(0);
			totalSum.setEndUnsolvedCount1(0);
			totalSum.setEndUnsolvedCount2(0);
			totalSum.setEndUnsolvedCount3(0);
			totalSum.setEndUnsolvedCountX(0);
			totalSum.setEndUnsolvedSumCount(0);			
			for(IncidentStateReport sr:result){
				//累加开始未解决的
				totalSum.setBeginUnSolvedCount1(totalSum.getBeginUnSolvedCount1()+(sr.getBeginUnSolvedCount1()==null?0:sr.getBeginUnSolvedCount1()));
				totalSum.setBeginUnSolvedCount2(totalSum.getBeginUnSolvedCount2()+(sr.getBeginUnSolvedCount2()==null?0:sr.getBeginUnSolvedCount2()));
				totalSum.setBeginUnSolvedCount3(totalSum.getBeginUnSolvedCount3()+(sr.getBeginUnSolvedCount3()==null?0:sr.getBeginUnSolvedCount3()));
				totalSum.setBeginUnSolvedCountX(totalSum.getBeginUnSolvedCountX()+(sr.getBeginUnSolvedCountX()==null?0:sr.getBeginUnSolvedCountX()));
				totalSum.setBeginUnSolvedSumCount(totalSum.getBeginUnSolvedSumCount()+(sr.getBeginUnSolvedSumCount()==null?0:sr.getBeginUnSolvedSumCount()));
				//累加收到的
				totalSum.setRecCount1(totalSum.getRecCount1()+(sr.getRecCount1()==null?0:sr.getRecCount1()));
				totalSum.setRecCount2(totalSum.getRecCount2()+(sr.getRecCount2()==null?0:sr.getRecCount2()));
				totalSum.setRecCount3(totalSum.getRecCount3()+(sr.getRecCount3()==null?0:sr.getRecCount3()));
				totalSum.setRecCountX(totalSum.getRecCountX()+(sr.getRecCountX()==null?0:sr.getRecCountX()));
				totalSum.setRecSumCount(totalSum.getRecSumCount()+(sr.getRecSumCount()==null?0:sr.getRecSumCount()));
				//累加解决的
				totalSum.setSolvedCount1(totalSum.getSolvedCount1()+(sr.getSolvedCount1()==null?0:sr.getSolvedCount1()));
				totalSum.setSolvedCount2(totalSum.getSolvedCount2()+(sr.getSolvedCount2()==null?0:sr.getSolvedCount2()));
				totalSum.setSolvedCount3(totalSum.getSolvedCount3()+(sr.getSolvedCount3()==null?0:sr.getSolvedCount3()));
				totalSum.setSolvedCountX(totalSum.getSolvedCountX()+(sr.getSolvedCountX()==null?0:sr.getSolvedCountX()));
				totalSum.setSolvedSumCount(totalSum.getSolvedSumCount()+(sr.getSolvedSumCount()==null?0:sr.getSolvedSumCount()));
				//累加周末还未解决的
				totalSum.setEndUnsolvedCount1(totalSum.getEndUnsolvedCount1()+(sr.getEndUnsolvedCount1()==null?0:sr.getEndUnsolvedCount1()));
				totalSum.setEndUnsolvedCount2(totalSum.getEndUnsolvedCount2()+(sr.getEndUnsolvedCount2()==null?0:sr.getEndUnsolvedCount2()));
				totalSum.setEndUnsolvedCount3(totalSum.getEndUnsolvedCount3()+(sr.getEndUnsolvedCount3()==null?0:sr.getEndUnsolvedCount3()));
				totalSum.setEndUnsolvedCountX(totalSum.getEndUnsolvedCountX()+(sr.getEndUnsolvedCountX()==null?0:sr.getEndUnsolvedCountX()));
				totalSum.setEndUnsolvedSumCount(totalSum.getEndUnsolvedSumCount()+(sr.getEndUnsolvedSumCount()==null?0:sr.getEndUnsolvedSumCount()));
			}
			result.add(totalSum);
		}
		return result;
	}

	public List<IncidentAnalysisReport> MBLQueryIncidentAnalysisGroupByType(long custId, Date expStartDate,
			Date expEndDate) throws Exception {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("custId", custId);
		params.put("expStartDate", expStartDate);
		params.put("expEndDate", expEndDate);
		List<IncidentAnalysisReport> datas = incidentAnalysisReportDAO.findByParams("IC_INCIDENT.queryIncidentAnalysisGroupByType", params);
		if(datas.size()>0){
			//todo 计算百分比、合计
			IncidentAnalysisReport totalSum = new IncidentAnalysisReport();
			totalSum.setIncidentType("小计");
			totalSum.setProdTypeCount1(0);
			totalSum.setProdTypeCount2(0);
			totalSum.setProdTypeCount3(0);
			totalSum.setProdTypeCount4(0);
			totalSum.setProdTypeCount5(0);
			for(IncidentAnalysisReport ar:datas){
				totalSum.setProdTypeCount1(totalSum.getProdTypeCount1()+(ar.getProdTypeCount1()==null?0:ar.getProdTypeCount1()));
				totalSum.setProdTypeCount2(totalSum.getProdTypeCount2()+(ar.getProdTypeCount2()==null?0:ar.getProdTypeCount2()));
				totalSum.setProdTypeCount3(totalSum.getProdTypeCount3()+(ar.getProdTypeCount3()==null?0:ar.getProdTypeCount3()));
				totalSum.setProdTypeCount4(totalSum.getProdTypeCount4()+(ar.getProdTypeCount4()==null?0:ar.getProdTypeCount4()));
				totalSum.setProdTypeCount5(totalSum.getProdTypeCount5()+(ar.getProdTypeCount5()==null?0:ar.getProdTypeCount5()));
			}
			//最后一行百分比
			IncidentAnalysisReport totalPercent = new IncidentAnalysisReport();
			totalPercent.setIncidentType("");
			totalPercent.setProdTypeCount1(totalSum.getProdTypeCount1()*100/totalSum.getProdTypeSumCount());
			totalPercent.setProdTypeCount2(totalSum.getProdTypeCount2()*100/totalSum.getProdTypeSumCount());
			totalPercent.setProdTypeCount3(totalSum.getProdTypeCount3()*100/totalSum.getProdTypeSumCount());
			totalPercent.setProdTypeCount4(totalSum.getProdTypeCount4()*100/totalSum.getProdTypeSumCount());
			totalPercent.setProdTypeCount5(100-(totalPercent.getProdTypeCount1()+totalPercent.getProdTypeCount2()+totalPercent.getProdTypeCount3()+totalPercent.getProdTypeCount4()));
			totalPercent.setPercent(100);
			//计算每行百分比
			int sumPercent = 0;
			for(int i=0;i<datas.size();i++){
				IncidentAnalysisReport ar = datas.get(i);
				//最后一行用100减去累加，确保所有的百分比加起来是100%
				if(i==datas.size()-1){
					ar.setPercent(100-sumPercent);
				}else{
					ar.setPercent(ar.getProdTypeSumCount()*100/totalSum.getProdTypeSumCount());
					sumPercent = sumPercent + ar.getPercent();
				}
			}
			datas.add(totalSum);
			datas.add(totalPercent);
		}
		return datas;
	}
	
	public static void main(String[] args) {
		System.out.println(7d/8);
	}

	public Map<Integer,Integer> MBLQueryIncidentSolveStateCount(long custId, Date expStartDate, Date expEndDate)
			throws Exception {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("custId", custId);
		params.put("expStartDate", expStartDate);
		params.put("expEndDate", expEndDate);
		List<StateCount> datas = stateCountDAO.findByParams("XC_INCIDENT.queryIncidentSolveStateCount", params);
		Map<Integer,Integer> result = new HashMap<Integer, Integer>();
		for(StateCount sc:datas){
			result.put(sc.getStateCode(), sc.getStateCount());
		}
		return result;
	}

	public Map<Integer,Integer> MBLQueryIncidentSatisfyStateCount(long custId, Date expStartDate, Date expEndDate)
			throws Exception {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("custId", custId);
		params.put("expStartDate", expStartDate);
		params.put("expEndDate", expEndDate);
		List<StateCount> datas = stateCountDAO.findByParams("XC_INCIDENT.queryIncidentSatisfyStateCount", params);
		Map<Integer,Integer> result = new HashMap<Integer, Integer>();
		for(StateCount sc:datas){
			result.put(sc.getStateCode(), sc.getStateCount());
		}
		return result;
	}
	
	
	
	
}
